package com.md.youyouhui.ui.user;

import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityReplacePhoneBinding;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by AdministratorZJ
 * on 2019/4/10.
 */
public class ReplacePhoneActivity extends BaseActivity {
    private ActivityReplacePhoneBinding phoneBinding;
    private Timer timer = new Timer();
    private TimerTask timerTask;
    private int time = 60;
    private MainViewModel viewModel;

    @Override
    public void setContentLayout() {
        phoneBinding = DataBindingUtil.setContentView(this, R.layout.activity_replace_phone);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(phoneBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        String phone = getIntent().getStringExtra("phone");
        phoneBinding.replacePhone.setText(String.format("+86%s", phone));
        phoneBinding.timeCode.setOnClickListener(v -> sendCodeService());
        phoneBinding.replaceBtn.setOnClickListener(v -> replacePhone());
    }

    private void replacePhone() {
        String phone = phoneBinding.replaceNewPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(ct, "请输入手机号码", Toast.LENGTH_SHORT).show();
            return;
        }
        String code = phoneBinding.replaceCode.getText().toString();
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(ct, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        viewModel.modifyPhone(phone, code).observe(this, objectResource -> {
            assert objectResource != null;
            switch (objectResource.status) {
                case LOADING:
                    phoneBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    phoneBinding.progress.setVisibility(View.GONE);
                    Toast.makeText(ct, "更换成功", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case ERROR:
                    phoneBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, objectResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    private void sendCodeService() {
        String phone = phoneBinding.replaceNewPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        viewModel.sendCode(phone).observe(this, codeResponse -> {
            assert codeResponse != null;
            switch (codeResponse.status) {
                case LOADING:
                    break;
                case SUCCESS:
                    sendCode();
                    break;
                case ERROR:
                    ToastUtils.errMake(this, codeResponse.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    private void sendCode() {
        timerTask = new TimerTask() {
            @Override
            public void run() {
                time--;
                runOnUiThread(() -> {
                    phoneBinding.timeCode.setText(String.format("%dS", time));
                    if (time == 0) {
                        time = 60;
                        phoneBinding.timeCode.setText("发送验证码");
                        phoneBinding.timeCode.setEnabled(true);
                        timerTask.cancel();
                    } else {
                        phoneBinding.timeCode.setEnabled(false);
                    }
                });
            }
        };
        timer.schedule(timerTask, 100, 1000);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null == timer) {
            return;
        }
        if (null != timerTask) {
            timerTask.cancel();
        }
        timer.purge();
        timer.cancel();
    }
}
